// pages/my/goldcoin/goldcoin.js

const App = getApp()
const api = require('../../../utils/api.js');

Page({
  data: {

    coinTotal: 0,

    goldcoin: {
      items: [
        /**
         * 
        {
          name:'淘淘领',
          time:'2017-10-31 22:45:10',
          goldcoin:'+200'
        },
        {
          name: '签到赠送',
          time:'2017-10-31 22:45:10',
          goldcoin: '-30'
        }
         */
      ],
      params: {
        page: 0,
        limit: 10,
      },
      paginate: {}
    },

    prompt: {
      hidden: 1,
      icon: '/image/empty.png',
      title: '您还没有金币！',
      text: '赶快去淘金币哦',
    },
  },

  onLoad(options) {

    this.setData({
      coinTotal: options.coinTotal
    })

    this.onPullDownRefresh()
  },


  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    console.info('onPullDownRefresh')

    this.initData()
    this.goldcoinList()
    //this.getList()

      // complete
    wx.showLoading({
      title: '加载中',
    })

    setTimeout(function () {
      wx.hideLoading()
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    }, 1500)
  },


  initData() {
    this.setData({
      goldcoin: {
        items: [],
        params: {
          page: 0,
          limit: 10,
        },
        paginate: {}
      }
    })
  },




  //金币流水
  goldcoinList: function (e) {

    let paramsData = {
      member_id: App.globalData.memberInfo.member_id,
      member_type_key: 'consumer',
    }


    let pageData = {
      page_no: this.data.goldcoin.params.page + 1,
      page_size: this.data.goldcoin.params.limit,
    }
      //金币列表接口
    const data = {
      service: 'finance.memberGoldLogPageFind',
      params: JSON.stringify(paramsData),
      page: JSON.stringify(pageData),
      url_type: 'finance',

      app_token: App.globalData.app_token,         //add
      security_code: App.globalData.security_code  //add

    }
    var that = this

    const goldcoin = this.data.goldcoin
    const prompt = this.data.prompt
    const params = goldcoin.params

    api.reqData({
      data,
      success: (res) => {
        var resData = res.data;


        var goldList = resData.data.list
        for (var i = 0; i < goldList.length; i++) {
          console.log(goldList[i].amount)
          //保留整数
          goldList[i].amount = parseInt(goldList[i].amount)
          //判断+号
          if (goldList[i].amount>0){
            goldList[i].amount = "+"+goldList[i].amount
            console.log(goldList[i].amount);
          }
        }

        goldcoin.items = [...goldcoin.items, ...goldList]
        goldcoin.params.page = resData.data.page.page_no
        goldcoin.params.limit = resData.data.page.page_size

        goldcoin.paginate.total = resData.data.page.total_count





        //判断hasNext
        if (goldcoin.paginate.total > goldcoin.params.page * goldcoin.params.limit) {
          goldcoin.paginate.hasNext = true
        }
        prompt.hidden = goldcoin.items.length


        this.setData({
          goldcoin: goldcoin,
          //'prompt.hidden': cash.items.length,
          prompt: prompt,
        })

      },
      fail: (res) => {
        console.log("fail 返回 res.data>>>" + res.data);
      }
    })
  },




  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    console.info('onReachBottom')
    if (!this.data.goldcoin.paginate.hasNext) return
    this.goldcoinList()
  },
})


